
import ECharts from 'vue-echarts/components/ECharts'
import 'echarts/lib/chart/bar'
import 'echarts/lib/chart/pie'
import 'echarts/lib/chart/line'
import 'echarts/lib/component/tooltip'
import 'echarts/lib/component/legend'
import 'echarts/lib/component/dataZoom'
import 'echarts/lib/component/toolbox'
import getRecentSalesVolume from './data/getRecentSalesVolume'
import getUserSource from './data/getUserSource'
import { parseTime } from '@/utils'
import { homeIndex } from '@/api/home'
export default {
  components: {
    'v-chart': ECharts
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', [new Date(), new Date()])
          }
        }, {
          text: '最近7天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近30天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '本月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            const days = start.getDate()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * (days - 1))
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '上月',
          onClick(picker) {
            const nowdays = new Date()
            let year = nowdays.getFullYear()
            let month = nowdays.getMonth()
            if (month === 0) {
              month = 12
              year = year - 1
            }
            if (month < 10) {
              month = '0' + month
            }
            var myDate = new Date(year, month, 0)
            const end = year + '-' + month + '-' + myDate.getDate()
            const start = year + '-' + month + '-01'
            // start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            // end.setTime(start.getTime() - 3600 * 1000 * 24 * days) // 上个月月底最后一天
            picker.$emit('pick', [start, end])
          }
        }]
      },
      parentData: {
        title: '首页'
      },
      showEdit: 1, // 1主页 2编辑
      addData: null,
      dateRange: [parseTime(new Date().getTime() - 3600 * 1000 * 24 * 7, '{y}-{m}-{d}'), parseTime(new Date(), '{y}-{m}-{d}')],
      recentSalesVolume: null,
      userSource: {}, // 用户来源
      userSourceEnum: ['微信', 'QQ', '贴吧', '知乎', '短信', '豆瓣', '一点资讯', '网页', '新浪微博', '网易', '今日头条', '百度搜索', '其他'], // 用户来源文字描述
      shopData: {}, // 门店数据
      tableData: [] // 销量Top10
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取页面数据
    async getData() {
      homeIndex({
        type: 0,
        startdate: this.dateRange[0], // 默认今天
        enddate: this.dateRange[1] // 默认今天
      }).then(res => {
        this.shopData = Object.assign({}, res.detail.shop)
        this.tableData = res.detail.top
        this.recentSalesVolume = getRecentSalesVolume(res.detail.order, this) // 最近销量
        this.userSource = getUserSource(res.detail.user, this) // 用记来源
      })
    },
    closeEditpage() {
      this.showEdit = 1
    }
  }
}
